package com.smartwebx.ha.dal.realtime.sqlprovider;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

import java.text.ParseException;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;

import com.smartwebx.ha.dal.config.OracleConfigInfo;

public class RainfallProvider {
	private String ST_PPTN_R = OracleConfigInfo.REALTIME_NAME + ".ST_PPTN_R";

	public String getSingleStatMoreDaySQL(Map<String, Object> params)
			throws ParseException {
		Date startDate = (Date) params.get("startDate");
		params.put("startDate", DateUtils.addHours(startDate, 8));

		Date endDate = (Date) params.get("endDate");
		params.put("endDate", DateUtils.addHours(endDate, 9));

		BEGIN();
		SELECT("TM,DYP val");
		FROM(ST_PPTN_R);
		WHERE("TM >= #{startDate}");
		WHERE("TM < #{endDate}");
		WHERE("to_char(TM,'HH24')=8");
		WHERE("STCD=#{stcd}");
		return SQL();
	}

	public String getSingleStatOneDayValSQL(Map<String, Object> params) {
		Date datePoint = (Date) params.get("datePoint");
		params.put("startDate", DateUtils.addHours(datePoint, 8));
		params.put("endDate", DateUtils.addHours(datePoint, 9));

		BEGIN();
		SELECT("TM,DYP val");
		FROM(ST_PPTN_R);
		WHERE("TM >= #{startDate}");
		WHERE("TM < #{endDate}");
		WHERE("STCD=#{stcd}");
		WHERE("DYP IS NOT NULL");
		return SQL();
	}

	public String getMoreStatMoreDayRainValSQL(Map<String, Object> params) {
		Date startDate = (Date) params.get("startDate");
		params.put("startDate", DateUtils.addHours(startDate, 8));
		Date endDate = (Date) params.get("endDate");
		params.put("endDate", DateUtils.addHours(endDate, 8));

		BEGIN();
		SELECT("STCD,DYP val,TM");
		FROM(ST_PPTN_R);
		WHERE("TM >= #{startDate}");
		WHERE("TM < #{endDate}");
		WHERE("to_char(TM,'HH24')=8");
		WHERE("STCD IN (#{stcds,typeHandler=InQueryHandler})");
		WHERE("DYP IS NOT NULL");

		return SQL();
	}

	public String getMoreStatOneDayRainValSQL(Map<String, Object> params) {
		Date datePoint = (Date) params.get("datePoint");
		params.put("startDate", DateUtils.addHours(datePoint, 8));
		params.put("endDate", DateUtils.addHours(datePoint, 9));

		BEGIN();
		SELECT("STCD,DYP val,TM");
		FROM(ST_PPTN_R);
		WHERE("TM >= #{startDate}");
		WHERE("TM < #{endDate}");
		WHERE("STCD IN (#{stcds,typeHandler=InQueryHandler})");
		WHERE("DYP IS NOT NULL");

		return SQL();
	}
}
